package com.sooth.common.dream.handler;

import com.dream.mate.tenant.inject.TenantHandler;
import com.dream.system.config.MethodInfo;
import com.dream.system.table.TableInfo;
import com.sooth.common.dream.annotation.IgnoreTenant;
import com.sooth.common.satoken.utils.LoginHelper;

import java.lang.reflect.Method;

public class SoothTenantHandler implements TenantHandler {
    @Override
    public boolean isTenant(MethodInfo methodInfo, TableInfo tableInfo) {
        boolean ignoreTenant = false;
        Method method = methodInfo.getMethod();
        if (method != null) {
            ignoreTenant = method.getAnnotation(IgnoreTenant.class) != null || method.getDeclaringClass().getAnnotation(IgnoreTenant.class) != null;
        }
        return !ignoreTenant && tableInfo.getType().getAnnotation(IgnoreTenant.class) == null && TenantHandler.super.isTenant(methodInfo, tableInfo);
    }

    @Override
    public Object getTenantObject() {
        return LoginHelper.getTenantId();
    }
}
